import numpy as np
from tensorflow.keras.models import load_model
from datetime import datetime
import joblib

# 加载模型和归一化器
loaded_model = load_model('model/cnn_gru_model.h5')
scaler_features = joblib.load('model/scaler_features.pkl')
scaler_target = joblib.load('model/scaler_target.pkl')

# 准备预测数据
# 默认时间特征
time_feature = '2019-01-01 15:45:00'
# 将时间字符串转换为 datetime 对象
# 16.96	-5.747	926.031	33.274	435.95	392.355	218.467
time_obj = datetime.strptime(time_feature, '%Y-%m-%d %H:%M:%S')
# 转换为时间戳（浮点数）
timestamp = time_obj.timestamp()
# 假设其他七个原始数据值是字符串形式的数值
other_features = ['16.96', '-5.747', '926.031', '33.274', '435.95', '392.355', '218.467']
# 转换为浮点数
other_features = [float(x) for x in other_features]
# 合并时间特征和其他特征
input_features = other_features

# 归一化输入特征
input_features = scaler_features.transform(np.array(input_features).reshape(1, -1))

# 准备输入序列
sequence_length = 10
input_sequence = np.repeat(input_features[np.newaxis, :, :], sequence_length, axis=1)

# 进行预测
prediction = loaded_model.predict(input_sequence)

# 反归一化预测结果
prediction = scaler_target.inverse_transform(prediction)

print(f"预测的发电功率为: {prediction[0][0]} mw")